1 00:00:00,600 --> 00:00:01,110 Okay. 2 00:00:01,110 --> 00:00:06,360 In this lecture, we're going to continue scripting for our game by having the player's camera rotate 3 00:00:06,360 --> 00:00:11,550 around the map when they are in the main menu, and then return the camera back to the player's character 4 00:00:11,550 --> 00:00:13,350 when they spawn into the map. 5 00:00:13,350 --> 00:00:17,790 We're going to create a dedicated local script for this task, and we're going to place it in starter 6 00:00:17,790 --> 00:00:18,510 player scripts. 7 00:00:18,510 --> 00:00:22,860 So we'll open up starter Player and in starter player scripts, we'll create a new local script. 8 00:00:22,860 --> 00:00:27,540 And I'm going to call this my camera handler. 9 00:00:28,790 --> 00:00:30,950 I'll use the same template as before. 10 00:00:31,430 --> 00:00:32,870 We'll get rid of this section. 11 00:00:32,870 --> 00:00:34,640 Get rid of that section. 12 00:00:35,350 --> 00:00:40,270 And inside of here is where we're going to want to be able to manipulate our camera so we can go ahead 13 00:00:40,270 --> 00:00:42,370 and make a reference to our camera. 14 00:00:42,400 --> 00:00:45,880 I'll just call a variable camera and that's equal to the workspace. 15 00:00:45,880 --> 00:00:51,040 And we can get our camera by referring to the current camera, which is the object being used by the 16 00:00:51,040 --> 00:00:51,970 local player. 17 00:00:52,510 --> 00:00:58,510 And then we also want to go ahead and grab that part in the workspace where we want to set our camera, 18 00:00:58,660 --> 00:01:00,880 um, position to be equal to. 19 00:01:00,910 --> 00:01:04,000 So that's going to be our camera part. 20 00:01:04,000 --> 00:01:05,830 And that's going to be equal to workspace. 21 00:01:05,830 --> 00:01:12,580 And inside of the filtering folder inside of the invisible folder we have our camera part. 22 00:01:13,220 --> 00:01:18,680 And when our player first joins the game, we want to go ahead and set the player's camera to be positioned 23 00:01:18,680 --> 00:01:22,040 at this camera part and then start rotating around the map. 24 00:01:22,040 --> 00:01:25,730 In order to do that, we're going to need the run service. 25 00:01:25,820 --> 00:01:27,740 So I'll get the run service. 26 00:01:28,600 --> 00:01:34,060 And we can connect to an event in the run service, like the heartbeat event, which fires every frame 27 00:01:34,060 --> 00:01:39,160 after the physics simulation to constantly rotate our player's camera around the map. 28 00:01:40,120 --> 00:01:45,460 So what we could do is inside of this main section, we will. 29 00:01:46,060 --> 00:01:51,850 Refer to our run service, get the heartbeat event, and we would connect a function to it that would 30 00:01:51,850 --> 00:01:53,440 rotate our camera around the map. 31 00:01:53,440 --> 00:01:55,900 So let's create a dedicated function for that. 32 00:01:55,900 --> 00:01:58,990 And we're going to call it rotate camera. 33 00:01:59,820 --> 00:02:05,550 And inside of here, all we're going to do is update the key frame of our camera to be equal to itself. 34 00:02:05,550 --> 00:02:10,320 But we're going to add an angle using key frame dot angles. 35 00:02:10,320 --> 00:02:13,530 And we want to rotate the camera around its y axis. 36 00:02:13,530 --> 00:02:17,010 So we're going to do zero for radians on the x axis. 37 00:02:17,010 --> 00:02:20,970 But then for radians on the y axis we're going to do math dot rad. 38 00:02:21,300 --> 00:02:24,090 We're going to get the camera's original rotation. 39 00:02:24,090 --> 00:02:31,020 So we're going to do camera dot key frame dot rotation and get its rotation on the y axis. 40 00:02:31,020 --> 00:02:36,510 And then we want to go ahead and subtract this value by some kind of number. 41 00:02:36,510 --> 00:02:40,590 And since this is going to be firing every single frame, we want this number to be quite small. 42 00:02:40,590 --> 00:02:43,620 So we could do something like 0.002. 43 00:02:44,010 --> 00:02:48,030 And then for the value on the Z we're going to do zero. 44 00:02:49,080 --> 00:02:54,330 And while we are rotating the camera around the map, we also want to make sure we set the camera's 45 00:02:54,330 --> 00:02:58,770 camera type mode, so specifies the camera type to be read by the camera scripts. 46 00:02:58,770 --> 00:03:03,000 We want to make sure we set this equal to the enum dot camera type dot scriptable. 47 00:03:03,000 --> 00:03:07,740 So it says no default behavior used when developers need to script custom behavior. 48 00:03:07,740 --> 00:03:10,260 So we're scripting custom behavior for our camera. 49 00:03:10,260 --> 00:03:12,120 We want it to rotate around the map. 50 00:03:12,120 --> 00:03:13,920 So that's what we're going to be doing. 51 00:03:13,920 --> 00:03:17,490 So we can connect rotate camera to the heartbeat event. 52 00:03:17,820 --> 00:03:24,330 And then basically when we don't want the camera to be rotating around the map, we could disconnect 53 00:03:24,330 --> 00:03:29,670 this function from our heartbeat event so it stops executing and then reset the player's camera back 54 00:03:29,670 --> 00:03:30,540 to their character. 55 00:03:30,540 --> 00:03:35,550 So that means we're going to have to store in a variable the connection return from our connect function. 56 00:03:35,550 --> 00:03:41,430 So we can create a variable up here I'm going to call it um camera connection. 57 00:03:41,930 --> 00:03:44,060 And we're not going to initialize it with any value. 58 00:03:44,060 --> 00:03:50,660 But down here, we're going to get our camera connection and set it equal to what gets returned from 59 00:03:50,660 --> 00:03:51,920 our connect function. 60 00:03:51,920 --> 00:03:55,160 Because then in the future, we want to disconnect it. 61 00:03:55,160 --> 00:04:00,230 We can simply refer to our camera connection and use the disconnect function it says disconnects the 62 00:04:00,230 --> 00:04:01,370 connection from the event. 63 00:04:02,030 --> 00:04:06,950 Now, since we have two different local script, one that's in our GUI, and then this one for manipulating 64 00:04:06,950 --> 00:04:12,260 the camera, we want to be able to communicate between these two different scripts, so we can tell 65 00:04:12,260 --> 00:04:16,730 this script when to start rotating the camera and when to stop rotating the camera. 66 00:04:16,730 --> 00:04:20,510 And that's why inside of replicated storage and our remotes. 67 00:04:20,510 --> 00:04:25,040 And then in our GUI folder I have a bindable event here called Rotate Camera. 68 00:04:25,040 --> 00:04:31,040 And this allows local scripts to communicate with our camera handler when to start or stop rotating 69 00:04:31,040 --> 00:04:31,760 the camera. 70 00:04:31,760 --> 00:04:34,970 So we can go ahead and make a variable to refer to that event. 71 00:04:34,970 --> 00:04:43,400 I will call it um, rotate camera event and that means we're going to have to get replicated storage. 72 00:04:43,400 --> 00:04:49,160 So replicated storage is equal to game get service replicated storage. 73 00:04:49,310 --> 00:04:54,140 And then we can set this equal to replicated storage dot remotes dot guy. 74 00:04:54,140 --> 00:04:57,860 And then get the rotate camera Bindable event. 75 00:04:58,250 --> 00:05:01,430 And then inside of our handler section we can go ahead and listen to our event. 76 00:05:01,430 --> 00:05:03,710 So rotate camera event. 77 00:05:04,410 --> 00:05:10,410 And then we can get the dot event it says fires when any script calls the fire method on the same bindable 78 00:05:10,410 --> 00:05:13,800 event instance, we can connect a function to that. 79 00:05:13,800 --> 00:05:19,620 And what we could do is we could expect any local script to pass us a boolean of whether or not we want 80 00:05:19,620 --> 00:05:21,600 to start or stop rotating the camera. 81 00:05:21,600 --> 00:05:25,470 So we'll create a parameter called bool, and this will be a boolean of true or false. 82 00:05:25,590 --> 00:05:30,150 And if this boolean is true, then that means we should start rotating our camera around the map. 83 00:05:30,150 --> 00:05:33,390 Otherwise we need to stop rotating our camera around the map. 84 00:05:33,390 --> 00:05:40,110 So if we want to stop rotating our camera around the map, then we would get our camera connection and 85 00:05:40,110 --> 00:05:42,960 we would disconnect it, which would stop the rotation. 86 00:05:42,960 --> 00:05:51,420 And then we would set the camera's camera type to be equal to the enum dot camera type dot custom says 87 00:05:51,420 --> 00:05:57,090 default mode used by Roblox core scripts, and by setting it back to the custom camera type, it's going 88 00:05:57,090 --> 00:06:00,180 to move the camera back to our players character. 89 00:06:00,980 --> 00:06:07,580 However, if we want to rotate it around the map, then what we're going to do is we're going to set 90 00:06:07,580 --> 00:06:11,990 camera, dot camera, type back to the camera type of scriptable. 91 00:06:12,050 --> 00:06:16,820 We want to set the camera C frame equal to our camera's part C frame. 92 00:06:16,820 --> 00:06:18,710 So camera part dot C frame. 93 00:06:18,710 --> 00:06:23,420 And then we want to go ahead and reconnect the rotate camera function back to the heartbeat event. 94 00:06:23,420 --> 00:06:28,550 So we're going to update camera connection to be equal to run service dot heartbeat. 95 00:06:28,550 --> 00:06:31,430 And we're going to connect rotate camera. 96 00:06:31,670 --> 00:06:37,130 Now before we do this we want to make sure that there isn't any previous connections stored in this 97 00:06:37,130 --> 00:06:37,880 variable. 98 00:06:37,880 --> 00:06:40,310 And if there is we want to go ahead and disconnect it. 99 00:06:40,310 --> 00:06:47,720 So if we have a value stored or if we have a connection and our camera connection variable, then we 100 00:06:47,720 --> 00:06:49,220 want to make sure we disconnect it. 101 00:06:49,220 --> 00:06:51,890 So we'll do camera connection disconnect. 102 00:06:51,890 --> 00:06:57,920 So that way we're okay to override the value in here with a new connection that's connected to our heartbeat 103 00:06:57,920 --> 00:06:58,460 event. 104 00:06:59,270 --> 00:07:03,440 And then since this script is going to execute when our player joins the game and we want the player's 105 00:07:03,440 --> 00:07:07,490 camera to start rotating around the map when they join the game, down here in our main section, we 106 00:07:07,490 --> 00:07:14,300 want to make sure we update the position or C frame of our camera to be equal to that position on our 107 00:07:14,300 --> 00:07:21,410 map, so we can get our camera, and we'll make sure we update the C frame of our camera equal to the 108 00:07:21,410 --> 00:07:22,790 camera part. 109 00:07:23,530 --> 00:07:24,400 Dot cframe. 110 00:07:24,400 --> 00:07:30,250 Now, before we do this, we also want to make sure that we update the camera type of our camera equal 111 00:07:30,250 --> 00:07:32,650 to the enum dot camera type of scriptable. 112 00:07:32,650 --> 00:07:37,960 That way we are able to update and move the keyframe of our camera to the correct position. 113 00:07:38,590 --> 00:07:42,790 So now that means when our player joins the game and their camera's rotating around the map, we want 114 00:07:42,790 --> 00:07:47,590 to stop the player's camera from rotating around the map when they press the play button and they spawn 115 00:07:47,590 --> 00:07:48,400 into the game. 116 00:07:48,400 --> 00:07:53,080 And then we also want to start rotating their camera back around the map when the player dies, and 117 00:07:53,080 --> 00:07:56,830 then the GUI or the main menu comes back on their screen. 118 00:07:56,830 --> 00:08:03,250 So back inside of our starting menu handler, what we can go ahead and do is get that rotate camera 119 00:08:03,250 --> 00:08:05,830 event to communicate with our camera handler. 120 00:08:05,830 --> 00:08:09,400 So we can call this rotate camera event. 121 00:08:09,400 --> 00:08:13,660 And that's in replicated storage dot remotes, dot GUI dot rotate camera. 122 00:08:14,410 --> 00:08:20,620 And then when we activate our play button, after we tell the server to respawn us, what we could do 123 00:08:20,620 --> 00:08:28,420 is get our rotate camera event and fire and let our camera handler know that, hey, we want to stop 124 00:08:28,420 --> 00:08:34,690 rotating the camera around the map so we we pass false to that event. 125 00:08:34,690 --> 00:08:40,360 However, when our player dies, right, we want to start rotating the camera back around the map again. 126 00:08:40,360 --> 00:08:45,400 So after we have waited for a character to be added, we can use our rotate, camera event and fire. 127 00:08:45,400 --> 00:08:50,440 And this time we could say, hey, please start rotating the camera back around the map again. 128 00:08:50,890 --> 00:08:55,960 So now if we go and playtest our game, our camera should be rotating around the map, and then we should 129 00:08:55,960 --> 00:09:00,040 be able to press the play button spawn in, and our camera should stop rotating. 130 00:09:00,040 --> 00:09:01,660 So if we go ahead and hit play. 131 00:09:03,880 --> 00:09:05,200 Looks like we got an error. 132 00:09:05,200 --> 00:09:09,580 Camera part is not a valid member of the filtering folder and in this folder. 133 00:09:09,580 --> 00:09:11,350 So let's go ahead and see what we did wrong. 134 00:09:11,890 --> 00:09:18,580 If we go and look in the workspace um camera part is a member of these folders. 135 00:09:18,580 --> 00:09:20,170 Oh I know what's wrong. 136 00:09:20,170 --> 00:09:22,060 If we look in the workspace. 137 00:09:22,060 --> 00:09:25,180 The problem is, is that we have streaming enabled. 138 00:09:25,180 --> 00:09:30,040 So streaming enabled is enabled by default for all new games you create. 139 00:09:30,040 --> 00:09:34,000 And since this is a small game we have I'm going to go ahead and disable it. 140 00:09:34,000 --> 00:09:40,390 But because streaming enabled was enabled um, all of these parts were replicated to our player over 141 00:09:40,390 --> 00:09:41,080 time. 142 00:09:41,080 --> 00:09:48,160 And that means when these scripts executed, our camera part was not immediately available to us in 143 00:09:48,160 --> 00:09:48,910 the workspace. 144 00:09:48,910 --> 00:09:54,100 But since we've disabled streaming now, when the player joins the game, when these scripts execute, 145 00:09:54,100 --> 00:09:59,410 everything in the workspace should be replicated to us, and that means it's okay for us to go ahead 146 00:09:59,410 --> 00:10:01,990 and directly index for our camera part. 147 00:10:01,990 --> 00:10:07,630 If we were using streaming enabled, then that means we would have to use the wait for child event to 148 00:10:07,630 --> 00:10:12,250 go ahead and wait for these different instances to replicate to our client. 149 00:10:12,250 --> 00:10:17,590 But again, since this is going to be a small map and a small game, there's no need for us to have 150 00:10:17,590 --> 00:10:18,880 streaming enabled. 151 00:10:19,150 --> 00:10:22,360 So now if we go and play test, our problem should be fixed. 152 00:10:22,780 --> 00:10:24,670 Okay, the error is gone and there we go. 153 00:10:24,670 --> 00:10:27,070 Our camera is now rotating around the map. 154 00:10:27,670 --> 00:10:29,770 And then if I go and play test the game. 155 00:10:30,800 --> 00:10:32,090 Our camera should have reset. 156 00:10:32,090 --> 00:10:32,960 Perfect. 157 00:10:32,960 --> 00:10:34,880 Our camera is back on our player. 158 00:10:35,480 --> 00:10:37,280 And then if my player were to die. 159 00:10:37,310 --> 00:10:42,110 So if I reset, our camera should be back rotating around the map again. 160 00:10:42,110 --> 00:10:43,070 Just like that. 161 00:10:43,070 --> 00:10:43,970 Perfect. 162 00:10:44,710 --> 00:10:49,660 So just like that, we've added camera rotation to our game. 163 00:10:49,690 --> 00:10:52,150 I'll see you in the next lecture.